# _*_coding:utf-8_*_
"""
   ATS 1.0(Automated Test Server, Version 1.0)Prober Mapping File parser,
   Developed by Ivy.Li 2013.03.18.
   (C)Copyright 2013 Hech. All rights reserved
"""
import configparser as ConfigParser
import os

# error messages define
INVAL_HEADER = 1001
INVAL_PINS = 1002
NO_PMF_FOUND = 1008


class PMF:
    """
    header: {}
    pins:{
        'pin_name': val
    }
    """

    def __init__(self, pmf=None):
        if pmf:
            self.load(pmf)
        else:
            self.clear()

    def clear(self):
        self.header = {}
        self.pins = {}
        self.error = 0

    def load(self, pmf):
        if not os.path.exists(pmf):
            self.error = NO_PMF_FOUND
            return
        self.clear()
        cp = ConfigParser.ConfigParser()
        cp.read(pmf)
        try:
            for k, v in cp.items('Header'):
                self.header[k] = v
        except:
            self.error = INVAL_HEADER
            return
        try:
            for k, v in cp.items('Pins'):
                tmp = v.split(',')
                type, val = tmp[0][1:], tmp[1][:-1]
                if type == 'INT':
                    self.pins[k] = int(val)
        except:
            self.error = INVAL_PINS
            return
